package com.kye.express.business.personal_my_bill.monthly_balance_bill.list;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.personal_my_bill.monthly_balance_bill.detail.MonthlyBalanceBillDetailActivity;
import com.kye.express.databinding.ItemMonthlyBalanceBillBinding;
import com.kye.model.bean.bill.MonthlyBalanceBillBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

import static com.kye.express.business.personal_my_bill.monthly_balance_bill.list.MyBillListActivity.REQUEST_CODE_MONTHLY_BALANCE_BILL_DETAIL;

public class MonthlyBalanceBillViewBinder extends ItemViewBinder<MonthlyBalanceBillBean, MonthlyBalanceBillViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_monthly_balance_bill, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull MonthlyBalanceBillBean billBean) {
        holder.bind(billBean);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemMonthlyBalanceBillBinding mBind;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemMonthlyBalanceBillBinding.bind(itemView);

            RxView.clicks(mBind.getRoot())
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            final Context context = itemView.getContext();
                            if (context != null && context instanceof Activity) {
                                MonthlyBalanceBillDetailActivity.startActivityForResult((Activity) context, mBind.getBean(), REQUEST_CODE_MONTHLY_BALANCE_BILL_DETAIL);
                            }
                        }
                    });
        }

        public void bind(MonthlyBalanceBillBean billBean) {
            mBind.setBean(billBean);
            mBind.executePendingBindings();
        }
    }
}
